const http = require('http');
const logic = require('./logic');
const path = require('path');
const fs = require('fs');

const {fetch} = require('whatwg-fetch');

const {  request } = require('request');

const unirest = require('unirest');



http.get('http://www.dianping.com/mylist/ajax/shoprank?cityId=4&shopType=10&rankType=popscore&categoryId=0', (res) => {
    if (res.statusCode === 200) {
        let rawData = '';
        res.on('data', (chunk) => rawData += chunk);
        res.on('end', () => {
            try {
                const parsedData = JSON.parse(rawData);
                var shops = [];
                //循环得出所有店铺地址
                parsedData.shopBeans.forEach(function (shop) {
                    let shopUrl = `http://www.dianping.com/shop/${shop.shopId}`;
                    shop.shopId = shop.shopId.toString();
                    
                    shops.push({
                        shopUrl: shopUrl,
                        shopId: shop.shopId
                    });

                    var p = path.join(process.cwd(), 'shops', `shop_${shop.shopId}_comment.json`);
                    
                    if (fs.existsSync(p)) {
                        var shopInfoString = fs.readFileSync(p).toString();
                        var shopInfo = JSON.parse(shopInfoString);
                        if (shopInfo && shopInfo.reviewAllDOList && shopInfo.reviewAllDOList.length > 0) {
                            console.log(`index: $ {index}   shopId:${shop.shopId} :   ${shopInfo.reviewAllDOList.length}`);
                            for (let index = 0; index < shopInfo.reviewAllDOList.length; index++) {
                                var review = shopInfo.reviewAllDOList[index];
                                //let memberUrl = `http://www.dianping.com/member/${review.user.userId}`;
                                //console.log(review.user.userId);
                                // const page = await browser.newPage();
                                // //page.on('response', callback);
                                // await page.goto(memberUrl);
                                // const datas = await page.evaluate(() => {
                                //     const anchors = Array.from(document.querySelectorAll(".col-link"));
                                //     return anchors.map(anchor => anchor.textContent);
                                // });
                                // users.push({
                                //     userId: review.user.userId,
                                //     userNickName: review.user.userNickName,
                                //     userFace: review.user.userFace,
                                //     userPower: review.user.userPower,
                                //     datas: datas
                                // });

                                shop.reviewAllDOList = shopInfo.reviewAllDOList;
                                //shop.reviewDataList = shopInfo.reviewDataList;
                                
                                let member = {
                                    userId: review.user.userId.toString(),
                                    userNickName: review.user.userNickName,
                                    userFace: review.user.userFace,
                                    userPower: review.user.userPower,
                                    vipLevel: review.vipLevel
                                    // datas: datas
                                };
                                shop.dishTagStrList = shopInfo.dishTagStrList;
                                shop.reviewCountPic = shopInfo.reviewCountPic;
                                shop.reviewCountAll = shopInfo.reviewCountAll;

                                // unirest.put(`http://127.0.0.1:7001/shop/${shop.shopId}`)
                                // .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                                // .send({
                                //     dishTagStrList: shopInfo.dishTagStrList,
                                //     reviewCountPic: shopInfo.reviewCountPic,
                                //     reviewCountAll: shopInfo.reviewCountAll,
                                // })
                                // .end(function (response) {
                                //     console.log(response.body);
                                // });  

                                unirest.post('http://127.0.0.1:7001/member')
                                .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                                .send(member)
                                .end(function (response) {
                                    console.log(response.body);
                                });  
                                // writeStream2.write(JSON.stringify(member));
                                
                                // console.log('datas:', datas);
                                // await sleep(1000);
                                // await page.close();
                            }
                        }
                    }
                    // console.log('shop', shop);
                    unirest.post('http://127.0.0.1:7001/shop')
                    .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                    .send(shop)
                    .end(function (response) {
                        try {
                            let res = JSON.parse(response.body)
                            if (res.success) {
                                
                            }else{
                                console.log(shop);
                            }
                        } catch (error) {
                            
                        }
                        
                         
                    });  

                });
                let promises = [];
                let datas = [];
                console.log(`total shop count:${shops.length}`);
                //logic(shops);
            } catch (e) {
                console.log(e.message);
            }
        });
    }
});


